var FontAwesome;
var Glyphicons;
//获取form表单数据的json数组
function getFormDataArray(formDom){
	var form_data = formDom.serializeArray();
	//数组类型要手动调整
	var m = [];
	$.each(form_data,function(i,field){
		m.push('"'+field.name+'":"'+decodeURIComponent(field.value,true)+'"');
	});
	return m;
}
$(function(){
	$.ajax({
  	  type: 'POST',
  	  url: '/getFontAweSome',
  	  success: function(data){
  		FontAwesome = data;
  	  }
  	});
	$.ajax({
	  	  type: 'POST',
	  	  url: '/getGlyphicons',
	  	  success: function(data){
	  		Glyphicons = data;
	  	  }
	  	});

    $.ajax({
    	  type: 'POST',
    	  url: '/getPermission',
    	  success: function(data){
    		  var menu = $(".sidebar-menu");
    		  createTree(data,menu);
    	  }
    	});
    $('#userHeadImgUploadBtn').click(function(){
    	$('#upload_user_image_Modal').modal("show");
    });
    var upload_user_image_submit_btn = $("#upload_user_image_submit_btn_id");
	upload_user_image_submit_btn.click(function(){
		$('#upload_user_image_Modal').modal("hide");
		$('#waiting_Modal').modal("show");
		$('#upload_user_image_form_id').ajaxSubmit(function(data){
	  		  var type;
			  if(data.status == 0){
				  type = BootstrapDialog.TYPE_SUCCESS;
			  }
			  else if(data == 1){
				  type = BootstrapDialog.TYPE_WARNING;
			  }
			  else{
				  type = BootstrapDialog.TYPE_DANGER;
			  }
			  $('#waiting_Modal').modal("hide");
			  BootstrapDialog.show({
					type: type,
					title: "执行结果",
				buttons: [ {
	                label: '关闭',
	                action: function(dialogItself){
	                    dialogItself.close();
	                }
	            }],
					message: data.msg
				});
			});
		});
});

function openPane(name){
	var section_id = name+'_section_id';

	if($('#'+section_id).length>0)
		return;
	var wrapper = $(".content-wrapper");
	
    $.ajax({
  	  type: 'POST',
  	  url: name,
  	  data: {},
  	  success: function(data){
  		  var section = $('<section id="'+section_id+'" class="content"></section>');
  		  var box = $(data);
  		  section.append(box);
  		  wrapper.prepend(section);
  		  
  		  var boxHeader = box.children('.box-header');
  		  if(boxHeader.length>0){
  		  var boxTools = boxHeader.children('.box-tools');
  		  if(boxTools.length>0){
  			  var collapseTrigger = boxTools.children('[data-widget="collapse"]');
  			  if(collapseTrigger.length>0){
  				collapseTrigger.attr('id',section_id+'_collapseBtn');
  			  }
  			  var removeTrigger = boxTools.children('[data-widget="remove"]');
			  if(removeTrigger.length>0){
				  removeTrigger.attr('id',section_id+'_removeBtn');
			  }
			  box.boxWidget({
	  			  animationSpeed: 500,
	  			  collapseTrigger:'#'+section_id+'_collapseBtn',
	  			  removeTrigger: '#'+section_id+'_removeBtn'
	  			});
			  box.on('removed.boxwidget', function(){
				  section.remove();
			  });
  		  }
  		  
  		  }		  
  	  }
  	});
}

function createTree(data,root){
	var comparator = function(obja,objb){
		if(obja.id>objb.id)
			return 1;
		else if(obja.id<objb.id)
			return -1;
		else
			return 0;
	}
	data.sort(comparator);
	var tree = [];
	for(var i=0;i<data.length;i++){
		var node = data[i];
		var parent_index = 0;
		var parent_flag = false;
		while(parent_index<tree.length){
			if(tree[parent_index].id === node.pid){
				parent_flag = true;
				break;
			}
			parent_index++;
		}
		if(node.parent){
			  var newParentDiv = $('<li class="treeview"><a href="#" onclick=\"openPane(\''+node.permission+'\')\"><i class="'+node.icon+'"></i> <span>'+node.name+'</span><span class="pull-right-container"><i class="fa fa-angle-left pull-right"></i></span></a>');
			  var treeDiv = $('<ul class="treeview-menu"></ul>');
			  if(parent_flag)
				  tree[parent_index].ele.append(newParentDiv);
			  else
				  root.append(newParentDiv);
			  newParentDiv.append(treeDiv);
			  node.ele = treeDiv;
			  tree.push(node);
		}
	   else{	
		  var newParentDiv = $('<li><a href="#" onclick=\"openPane(\''+node.permission+'\')\"><i class="'+node.icon+'"></i> <span>'+node.name+'</span></a></li>');
		  node.ele = newParentDiv;
		  if(parent_flag)
			  tree[parent_index].ele.append(newParentDiv);		  
		  else
			  root.append(newParentDiv);
		  tree.push(node);
	   }
	}
}